import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.util.ArrayList;

import protocol.print.PrintRequest;
import protocol.registration.PrinterRegistrationRequest;
import protocol.registration.PrinterRegistrationResponse;
import protocol.registration.RegistrationRequest;
import protocol.registration.RegistrationResponse;
import protocol.registration.TypeRequest;


public class TCPClient extends Client{

	public static final int NumberOfClients = 1;
	public static final int NumberOfMessages = 1;
	
	private String serverIP;
	private int serverPort;
	
	public TCPClient(String serverIP, int serverPort) {
		this.serverIP = serverIP;
		this.serverPort = serverPort;
	}
	
	@Override
	public void run()
	{
		Socket clientSocket = null;
		try {
			
			//Conectez CoreServer
			clientSocket = new Socket(this.serverIP, this.serverPort);
		    RegistrationRequest rr  = new RegistrationRequest(m_ID, 5001, m_IP , m_zona);
		    rr.write(new DataOutputStream(clientSocket.getOutputStream()));
		    
		    RegistrationResponse rrt = new RegistrationResponse();
		    rrt.read(new DataInputStream(clientSocket.getInputStream()));
		    
		    //Am luat id 
		    m_ID = rrt.getID();
		  	this.serverIP = rrt.getIpServer();
		  	this.serverPort = rrt.getPort();
		  	
		  	
		    //Ma reconectez la serverul meu
		    clientSocket.close();
		    
		  	clientSocket = new Socket(this.serverIP, this.serverPort);
		    rr  = new RegistrationRequest(m_ID, 5002, m_IP , m_zona);
		    rr.write(new DataOutputStream(clientSocket.getOutputStream()));
		    
		    rrt = new RegistrationResponse();
		    rrt.read(new DataInputStream(clientSocket.getInputStream()));
	
		    this.serverPort = rrt.getPort();
		    clientSocket.close();
		    System.out.println(this.serverPort);
		    System.out.println(m_ID);
		    
		    Thread.sleep(5000);
		    //inregistrrez imprimanta
		    clientSocket = new Socket(this.serverIP, this.serverPort);
		     
		    TypeRequest tr = new TypeRequest(1);
		    
		    tr.write(new DataOutputStream(clientSocket.getOutputStream()));
		    
		    PrinterRegistrationRequest prr = new PrinterRegistrationRequest(m_ID,3,m_ID);
		    	
		    prr.write(new DataOutputStream(clientSocket.getOutputStream())); 
		    
		    System.out.println("Imprimanta a fost inregistrata : " + (new PrinterRegistrationResponse().read(new DataInputStream(clientSocket.getInputStream()))));
		    
		    Thread.sleep(10000);
		    
		    tr = new TypeRequest(0);
		    
		    tr.write(new DataOutputStream(clientSocket.getOutputStream()));
		    
		    
		    System.out.println("Trimit la printat");
		    PrintRequest toPrint = new PrintRequest(m_ID, 3 , "Textul Meu".getBytes());
		    toPrint.write(new DataOutputStream(clientSocket.getOutputStream()));
		    while(true)	{
		    	
		    }
		    

		} catch (Exception e) {
			System.err.println("TCPClient - exception: " + e);
			e.printStackTrace();
		}
		finally
		{
			try {
				clientSocket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
